// Variables
// (c) jimon game studio

#include "CVariables.h"

namespace je
{
	namespace core
	{
		IMPLEMENT_SINGLETON(Variables,JE_ENGINE_FUNCTION_CALL)

		//! Constructor
		CVariables::CVariables()
		{
			SetVariables(this);
		}

		//! Destructor
		CVariables::~CVariables()
		{
			for(VariablesList::Iterator Iter = Variables.begin();Iter != Variables.end();Iter++)
				JEDELETE(*Iter)
			Variables.clear();

			SetVariables(NULL);

		}

		//! Set Variable
		void CVariables::SetVariable(const jeStringc & Name,SystemVariableType Type,void * Data)
		{
			jeVariable * Variable = GetVariable(Name);

			if(Variable)
			{
				Variable->Type = Type;
				Variable->DeleteData();
				Variable->Data = Data;
			}
			else
			{
				JENEW(Variable,jeVariable)
				Variable->Name = Name;
				Variable->Type = Type;
				Variable->Data = Data;

				Variables.push_back(Variable);
			}
		}

		//! Get Variable
		jeVariable * CVariables::GetVariable(const jeStringc & Name)
		{
			for(VariablesList::Iterator Iter = Variables.begin();Iter != Variables.end();Iter++)
				if((*Iter)->Name == Name)
					return *Iter;
			
			return NULL;
		}
	}
}
